var util = require('../../utils/util.js');
var api = require('../../config/api.js');
var app = getApp();
var pageNo = 1,dataList = [],isLoading=false;
Page({
  data: {
    dataList: []
  },
  onShow: function (options) {

  },
  onLoad:function(){
    dataList = [];
    this.setData({
      dataList:dataList
    })
    pageNo = 1;
    isLoading = false;
    this.loadList();
  },
  //页面上拉触底事件的处理函数
  onReachBottom: function () {
    this.loadList();
  },
  //获取优惠券列表
  loadList: function () {
    if(isLoading){
        return;
    }
    var that = this;
    isLoading = true;
    wx.showLoading({title: '加载中...'});
    setTimeout(function(){

      var data = {
        pageNo: pageNo
      }
      util.request(api.CouponTypeList, data, 'POST').then(function (res) {
        wx.hideLoading();
        wx.stopPullDownRefresh();

        pageNo = res.pageNo;
        if(pageNo==res.totalPages){
            isLoading = true;
        }else{
            isLoading = false;
        }
        for(var i = 0; i < res.listData.length; i++){
            dataList.push(res.listData[i]);
        }
        that.setData({
            dataList:dataList,
            hasData:dataList.length>0
        });
        pageNo++;
      })
      
    },400);
  },
  //领取优惠券
  getCoupon: function (e) {
    var couponTypeId = e.currentTarget.dataset.couponTypeId;
    var data = {
      couponTypeId: couponTypeId
    }
    util.request(api.CouponReceive, data, 'POST').then(function (data) {
      if (data.type == 'success') {
        util.showSuccessToast(data.content);
      } else {
        util.showErrorToast(data.content);
      }
    });
  }

})